package com.itheima.controller.admin;


import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.config.RedisConfiguration;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Set;
import java.util.UUID;

/**
 * 体验套餐管理
 */

@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐管理")
@Slf4j
public class SetmealController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 套餐分页查询
     */
    @PostMapping("/findPage")
    @ApiOperation("分页查询套餐管理")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 新增体检套餐
     *
     * @param checkgroupIds
     * @param setmeal
     */
    @PostMapping("/add")
    @ApiOperation("新增体检套餐")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.add(setmeal, checkgroupIds);

            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    /**
     * 根据套餐id查询套餐数据
     *
     * @param id
     */
    @GetMapping("/findById")
    @ApiOperation("根据套餐id查询套餐数据")
    public Result findById(Integer id) {
        try {
            Setmeal setmeal = setmealService.findById(id);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     * 编辑体检套餐
     *
     * @param setmeal
     * @param checkGroupIds
     */
    @PostMapping("/edit")
    @ApiOperation("编辑体检套餐")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkGroupIds) {
        try {
            setmealService.edit(setmeal, checkGroupIds);
            return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }



    /**
     * 图片上传
     *
     * @param
     */
    @PostMapping("/upload")
    @ApiOperation("图片上传")
    public Result upload(MultipartFile imgFile) {
        try {
            // 获取原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            //截取原始文件名后缀
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //生成随机文件名
            String objectName = UUID.randomUUID().toString() + extension;
            //上传图片
            String path = aliOssUtil.upload(imgFile.getBytes(), objectName);

            // 保存图片名称到 Redis 集合 all_uploaded_pics
            redisTemplate.opsForSet().add("all_uploaded_pics", objectName);//  保存图片名称到 Redis 集合 all_uploaded_pics
            log.info("图片上传成功，图片路径为：{}", path);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, path);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

}
